<template>
  <div>
    <!--条件筛选-->
    <TopFilter ref="topFilter" :list="topFilterList" :cols="3" labelWidth="110" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      :isSelectColumn="false"
      :onRowSelectChange="selectChange"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <el-button size="small" type="primary" icon="el-icon-plus" @click="detailHandle">新增</el-button>
      </template>
    </FilterTable>

    <!-- 编辑窗口 -->
    <BaseDialog
      :visible.sync="visibleDrawer"
      :title="title"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <part-info v-model="initData" :type="handleType" :rowData="formData" @close="infoClose" />
    </BaseDialog>

  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import {getSortList} from '@/api/partsmanage/partinfos/partInfoManage/index';
import { getCurrentUser } from '@/api/cms-api/origin/construct/dealer';
import partInfo from './updatePartSort';

export default {
  name: "partSort",
  components: {partInfo},
  mixins: [dictionary],
  data() {
    return{
      topFilterList: this.createTopFilterList(),
      columns: this.createTableColumns(),
      fetchApi: getSortList,
      fetchParams: {
        sort: 1,
        xhrAbort: true
      },
      visibleDrawer: false,
      handleType: '',
      formData: {},
      title: '',
      initData: {},
      partinfos: {},
      rowSelectData: [],
    };
  },
  async created() {
    this.columns.find(x => x.dataIndex === 'bjpjsx').hidden = true
    this.columns.find(x => x.dataIndex == 'bjpjsx').dictItems = this.createDictList('5000')
    this.columns.find(x => x.dataIndex == 'bjpjfl').dictItems = this.createDictList('5017')
    // 获取当前登录人信息
    await getCurrentUser({}).then(response=>{
      console.info(response)
      if (response.data.appId === 'beijing'){
        console.info(this.topFilterList)
        this.topFilterList.find(x => x.fieldName === 'bjpjsx').hidden = false
        this.columns.find(x => x.dataIndex === 'bjpjsx').hidden = false
      }
    })
  },
  methods: {
    createTopFilterList() {
      return[
        {
          type: 'INPUT2',
          label: '配件代码',
          fieldName: 'bjxmbh'
        },
        {
          type: 'INPUT',
          label: '配件名称',
          fieldName: 'bjzwmc'
        },
        {
          type: 'SELECT',
          label: '配件属性',
          fieldName: 'bjpjsx',
          placeholder: '请选择...',
          itemList: this.createDictList('5000'),
          hidden: true
        },
        {
          type: 'SELECT',
          label: '配件分类',
          fieldName: 'bjpjfl',
          placeholder: '请选择...',
          itemList: this.createDictList('5017'),
        },
      ]
    },
    createTableColumns () {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 70,
          fixed: 'left',
          render: props => {
            return (
              <div>
                <el-button size="mini" type="text" onClick={() => this.editHandle(props.row, 'edit')}>
                  编辑
                </el-button>
              </div>
            );
          }
        },
        {
          title: '配件代码',
          dataIndex: 'bjxmbh',
          sorter: true,
        },
        {
          title: '配件名称',
          dataIndex: 'bjzwmc',
          sorter: true,
        },
        {
          title: '配件属性',
          dataIndex: 'bjpjsx',
          sorter: true,
        },
        {
          title: '配件分类',
          dataIndex: 'bjpjfl',
          sorter: true,
        },
      ]
    },
    selectChange(val) {
      console.log('val', val);
      this.rowSelectData = val;
    },
    detailHandle(type) {
      this.title = '新增';
      this.handleType = 'add';
      this.initData = {
        type: type, // add-edit-show
        id: 1001
      }; //行id
      this.visibleDrawer = true;
    },
    editHandle(row, type) {
      if (type === 'edit') {
        this.title = '编辑';
      }
      this.formData = row;
      this.handleType = type;
      this.visibleDrawer = true;
    },
    changeHandle(val) {
      this.fetchParams = Object.assign({}, val);
      this.fetchParams.sort = 1
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    infoClose(rData) {
      this.visibleDrawer = false;
      console.info(rData)
      if (rData.type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },
  }
}
</script>

<style scoped>

</style>
